//
//  WBComposeView.swift
//  sinaWeibo
//
//  Created by 吴熠 on 16/11/19.
//  Copyright © 2016年 吴熠. All rights reserved.
//

import UIKit
import pop


class WBComposeView: UIView {
    
    var composeBtnArr : [WBComposeButton] = [WBComposeButton]()
    
    //view展示的方法
    func composeViewShow(fromVC:UIViewController?){
        fromVC?.view.addSubview(self)
        //开始动画
        for (index,btn) in self.composeBtnArr.enumerated(){
            self.startAnimation(btn: btn, index: index,isShow:true)
        }
    }
    //视图加载window上的整个屏幕
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //点击动画消失
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        self.removeFromSuperview()
        for (index,btn) in composeBtnArr.reversed().enumerated(){
            self.startAnimation(btn: btn, index: index, isShow: false)
        }
        //动画完成后移除视图
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5) {
            self.removeFromSuperview()
        }
    }
    

    
    //展示动画  上来还是下去的动画
    private func startAnimation(btn:WBComposeButton,index:Int,isShow:Bool = true){
        
            let spingAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            spingAnimation?.springBounciness = 10.0
            spingAnimation?.springSpeed = 10.0
            //延迟时间
            spingAnimation?.beginTime = CACurrentMediaTime() + Double(index)*0.025
            let btnCenterY = btn.center.y+CGFloat(isShow ? -350 : 350)
            spingAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
            btn.pop_add(spingAnimation, forKey: nil)
        
    }

    //点击6个按钮的事件
    func didClickBtn(btn : WBComposeButton){
        
        UIView.animate(withDuration: 0.5, animations: {
            for button in self.composeBtnArr {
                if btn == button {
                    button.transform = CGAffineTransform(scaleX: 1.8, y: 1.8)
                }else{
                    button.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
                }
            }
        }) { (_) in
            
            //要转成string类型,下面根据string获取类型再实例化控制器
            let bundleName = Bundle.main.infoDictionary?["CFBundleName"] as! String
            let className = bundleName+"."+"WBComposeController"
            
            //获取类型
            guard let classType = NSClassFromString(className) as? UIViewController.Type else{
                return
            }
            let vc = classType.init()
//            let vc = WBComposeController()
            let nav = UINavigationController(rootViewController: vc)
            UIApplication.shared.keyWindow?.rootViewController?.present(nav, animated: true, completion: { 
                //跳转完毕之后将界面移除
                self.removeFromSuperview()
            })
//            print("完成了")
        }
        
    }

    
    private func setupUI(){
        //添加被处理的毛玻璃的背景,截取屏幕
//        let toolBar = UIToolbar(frame: UIScreen.main.bounds)
//        toolBar.barStyle = .black
//        self.addSubview(toolBar)
        self.addSubview(backView)
        self.addSixButton()
        self.setButtons()
    }
    
    private func setButtons(){
        let images = ["tabbar_compose_idea","tabbar_compose_camera","tabbar_compose_review","tabbar_compose_lbs","tabbar_compose_shooting","tabbar_compose_more"]
        
        let titles = ["文字","照片/视频","头条文章","签到","直播","更多"]
        
        for (index,btn) in composeBtnArr.enumerated(){
            btn.setImage(UIImage(named:images[index]), for: .normal)
            btn.setTitle(titles[index], for: .normal)
        }
    }

    
    func addSixButton(){
        for i in 0..<6{
            let btn = WBComposeButton()
//            btn.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
//            let btnW : CGFloat = 80
//            let btnH : CGFloat = 110
            let margin : CGFloat = (kScreenW - 3*btnW)/4
            let coloumIndex = i%3
            let rowIndex = i/3
            let x  = margin + (btnW + margin)*CGFloat(coloumIndex)
            let y = (btnH + margin ) * CGFloat(rowIndex)
            btn.frame = CGRect(x: x, y: y+kScreenH, width: btnW, height: btnH)
//            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_review"), for: .normal)
//            btn.setTitle("文字", for: .normal)
            //给btn添加点击事件
            btn.addTarget(self, action: #selector(didClickBtn(btn:)), for: .touchUpInside)
            
            self.addSubview(btn)
            composeBtnArr.append(btn)
        }
    }

    
    
    
    
    
    private lazy var backView : UIImageView = UIImageView(image: UIImage.snapShotCurrentWindow().applyLightEffect())
    
    


    
}
